﻿using System;
using System.IO;
using System.Windows.Forms;
using FBroSharp.Const;
using FBroSharp.DataType;
using FBroSharp;
using BaseTest;

namespace Cookie管理浏览器
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            // ====================================
            // FBro 框架初始化配置
            // ====================================
            
            // 缓存路径
            string baseCachePath = Path.Combine(Directory.GetCurrentDirectory(), "Cache");
            
            // 日志级别
            FBroSharpLogSeverity logSeverity = FBroSharpLogSeverity.ERROR;
            
            // 创建初始化配置对象
            FBroSharpInitSet set = new FBroSharpInitSet
            {
                //设置缓存目录
                cache_path = Path.Combine(baseCachePath, "CachePath") + "\\",

                //设置用户目录
                user_data_path = Path.Combine(baseCachePath, "UserPath") + "\\",

                //设置根缓存目录
                root_cache_path = baseCachePath + "\\",

                //启用事件消息循环
                multi_threaded_message_loop = true,

                //设置执行子进程
                browser_subprocess_path = Path.Combine(Directory.GetCurrentDirectory(), "FBroSubprocess.exe"),

                //本地化语言
                locale = "zh-CN",

                //保持cookie
                persist_session_cookies = true,

                //日志模式
                log_severity = logSeverity
            };

            //创建初始化事件
            InitEvent init_event = new InitEvent();

            //执行初始化
            if (!FBroSharpInitControl.InitPro(set, init_event))
            {
                MessageBox.Show(
                    "FBro 框架初始化失败！\n请检查：\n" +
                    "1. FBroSubprocess.exe 是否存在\n" +
                    "2. 缓存目录是否可写\n" +
                    "3. 没有其他 FBro 实例正在运行",
                    "初始化错误",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                );
                return;
            }

            Console.WriteLine("✅ FBro 框架初始化成功");
            Console.WriteLine($"   缓存路径: {baseCachePath}");
            Console.WriteLine($"   语言设置: zh-CN");
            Console.WriteLine($"   日志级别: ERROR");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            //关闭框架释放资源
            FBroSharpInitControl.Shutdown(false);
            
            Console.WriteLine("✅ FBro 框架关闭完成");
        }
    }
}
